package com.dc.bobaoge.project.v3.to.v2.business.core;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dc.bobaoge.base.entity.core.po.AuthenticationInfo;
import com.dc.bobaoge.base.entity.core.po.AuthenticationInfoHistory;
import com.dc.bobaoge.base.entity.core.po.AuthenticationInfoStore;
import com.dc.bobaoge.base.entity.core.po.AuthenticationInfoUpdate;
import com.dc.bobaoge.base.mapper.core.AuthenticationInfoHistoryMapper;
import com.dc.bobaoge.base.mapper.core.AuthenticationInfoMapper;
import com.dc.bobaoge.base.mapper.core.AuthenticationInfoStoreMapper;
import com.dc.bobaoge.base.mapper.core.AuthenticationInfoUpdateMapper;
import com.dc.bobaoge.common.em.UpdateAuthenticationInfoEnum;
import com.dc.bobaoge.project.base.core.constant.AuthInfoConstant;
import com.dc.bobaoge.project.base.core.constant.BaseInfoConstant;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreCerCompany;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreCerLevelSub;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreCerMessage;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreCerPayDeposit;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreCerPayLogDeposit;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreCerPerson;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreCerCompanyMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreCerLevelMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreCerLevelSubMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreCerMessageMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreCerPayDepositMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreCerPayLogDepositMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreCerPersonMapper;
import com.dc.bobaoge.project.v3.to.v2.business.core.util.AuthenticationInfoUtil;
import com.own.business.pay.common.em.PayEnum;
import com.own.business.pay.entity.po.BillHistory;
import com.own.business.pay.mapper.BillHistoryMapper;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import com.own.component.common.exception.BusinessSimpleException;
import com.own.component.common.util.list.ListUtil;
import com.own.constant.ConstantNumber;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

/**
 * AuthenticationInfoUpdateResolveMethod
 * <p>
 * 可触达的旧版系统审核状态
 * 理论上单个用户只会存在一个审核中的状态
 * 仅判断当前查询到的数据的最新一条数据
 * <p>
 * 根据主键id查询，bd_cer_msg 有没有对应的数据，如果有则修改，无则添加
 *
 * <p>
 * <table class="striped">
 * <caption>审核状态的触发</caption>
 * <thead>
 *  <tr><th scope="col">目标的审核状态</th><th scope="col">逻辑</th>
 * </thead>
 * <tbody>
 *   <tr><th scope="row">待审核</th><td></td></tr>
 *   <tr><th scope="row">审核通过</th><td></td></tr>
 *   <tr><th scope="row">审核拒绝</th><td> </td></tr>
 * </tbody>
 * </table>
 * <p>
 *
 * @author chenxueli
 * @date 2024-01-27 09:42:05
 */
@SuppressWarnings("all")
@Slf4j
@Component
public class AuthenticationInfoUpdateResolveMethod implements ResolveMethod {

    public static final Map<String, String> PAY_METHOD_MAP = new HashMap<>() {{
        put(PayEnum.Method.ALIPAY.getType(), "alipay_wap");
        put(PayEnum.Method.WECHAT.getType(), "wx_wap");
        put(PayEnum.Method.BALANCE.getType(), "yue");
    }};
    private static final String DEFAULT_TAG = "厂";
    private static final Map<UpdateAuthenticationInfoEnum, BiConsumer<OldCoreCerPerson, String>> PERSON_MAP = new HashMap<>();
    private static final Map<UpdateAuthenticationInfoEnum, BiConsumer<OldCoreCerCompany, String>> COMPANY_MAP = new HashMap<>();

    static {
        PERSON_MAP.put(UpdateAuthenticationInfoEnum.AVATAR, OldCoreCerPerson::setCerPersonImg);
        PERSON_MAP.put(UpdateAuthenticationInfoEnum.NAME, OldCoreCerPerson::setCerPersonName);
        PERSON_MAP.put(UpdateAuthenticationInfoEnum.INTRODUCE, OldCoreCerPerson::setCerPersonDetails);
        PERSON_MAP.put(UpdateAuthenticationInfoEnum.SCENE, OldCoreCerPerson::setCerPersonPremises);
        PERSON_MAP.put(UpdateAuthenticationInfoEnum.BUSINESS_SCOPE, OldCoreCerPerson::setCerPersonRange);
        PERSON_MAP.put(UpdateAuthenticationInfoEnum.BANK_CARD, OldCoreCerPerson::setCerPersonBankcard);
        PERSON_MAP.put(UpdateAuthenticationInfoEnum.QUALIFICATION_PICTURE, OldCoreCerPerson::setCerPersonImg);
        PERSON_MAP.put(UpdateAuthenticationInfoEnum.SHOP_ADDRESS, OldCoreCerPerson::setCerPersonAddress);

        COMPANY_MAP.put(UpdateAuthenticationInfoEnum.AVATAR, OldCoreCerCompany::setCerCompanyImg);
        COMPANY_MAP.put(UpdateAuthenticationInfoEnum.NAME, OldCoreCerCompany::setCerCompanyName);
        COMPANY_MAP.put(UpdateAuthenticationInfoEnum.INTRODUCE, OldCoreCerCompany::setCerCompanyDetails);
        COMPANY_MAP.put(UpdateAuthenticationInfoEnum.SCENE, OldCoreCerCompany::setCerCompanyPremises);
        COMPANY_MAP.put(UpdateAuthenticationInfoEnum.BUSINESS_SCOPE, OldCoreCerCompany::setCerCompanyRange);
        COMPANY_MAP.put(UpdateAuthenticationInfoEnum.BANK_CARD, OldCoreCerCompany::setCerCompanyBankcard);
        COMPANY_MAP.put(UpdateAuthenticationInfoEnum.QUALIFICATION_PICTURE, OldCoreCerCompany::setCerCompanyImg);
        COMPANY_MAP.put(UpdateAuthenticationInfoEnum.SHOP_ADDRESS, OldCoreCerCompany::setCerCompanyAddress);
        COMPANY_MAP.put(UpdateAuthenticationInfoEnum.PRODUCT, OldCoreCerCompany::setCerCompanyProduct);
    }

    @Resource
    private AuthenticationInfoMapper authenticationInfoMapper;
    @Resource
    private AuthenticationInfoUpdateMapper authenticationInfoUpdateMapper;
    @Resource
    private AuthenticationInfoHistoryMapper authenticationInfoHistoryMapper;
    @Resource
    private AuthenticationInfoStoreMapper authenticationInfoStoreMapper;
    @Resource
    private OldCoreCerMessageMapper oldCoreCerMessageMapper;
    @Resource
    private OldCoreCerPersonMapper oldCoreCerPersonMapper;
    @Resource
    private OldCoreCerCompanyMapper oldCoreCerCompanyMapper;
    @Resource
    private OldCoreCerLevelSubMapper oldCoreCerLevelSubMapper;
    @Resource
    private OldCoreCerLevelMapper oldCoreCerLevelMapper;
    @Resource
    private OldCoreCerPayLogDepositMapper oldCoreCerPayLogDepositMapper;
    @Resource
    private OldCoreCerPayDepositMapper oldCoreCerPayDepositMapper;

    @Resource
    private AuthenticationInfoUtil authenticationInfoUtil;
    @Resource
    private BillHistoryMapper billHistoryMapper;

    /**
     * 修改的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        if (trigger.getNewStatus() != null && trigger.getNewStatus() != AuthInfoConstant.NewUpdateStatus.PASS) {
            log.debug("不是通过状态，不处理:{},{}", trigger.getNewStatus(), trigger.getTargetId());
            return;
        }
        var update = authenticationInfoUpdateMapper.selectById(trigger.getTargetId());
        if (update == null) {
            throw new BusinessSimpleException("未查询到认证信息的更新数据");
        }
        if (update.getAuditStatus() != AuthInfoConstant.NewUpdateStatus.PASS) {
            log.debug("不是通过状态，不处理:{},{}", update.getAuditStatus(), trigger.getTargetId());
            return;
        }
        // 查询最新的认证信息
        var authInfo = getAuthInfoByUserId(update.getFkUserId());
        if (authInfo == null) {
            throw new BusinessSimpleException("未查询到认证信息");
        }
        // 查询旧版是否存在
        var oldCoreCerMessage = oldCoreCerMessageMapper.selectById(update.getId());
        if (oldCoreCerMessage == null) {
            add(trigger, authInfo, update);
        } else {
            update(trigger, authInfo, update, oldCoreCerMessage);
        }
    }

    /**
     * 添加
     *
     * @param trigger  触发器
     * @param authInfo 认证信息
     * @param update   更新数据
     */
    private void add(SyncTrigger trigger, AuthenticationInfo authInfo, AuthenticationInfoUpdate update) {
        var status = calcStatus(update);
        var searchId = 0L;
        if (authInfo.getVipType() == 1) {
            var person = new OldCoreCerPerson();
            person.setCerUserId(authInfo.getFkUserId());
            person.setCerUserNickname(authInfo.getNickname());
            person.setCerPersonName(authInfo.getName());
            person.setCerPersonPhone(authInfo.getPhone());
            person.setCerPersonIdcard(authInfo.getCertificateNo());
            person.setCerPersonBank("");
            person.setCerPersonBankcard(StringUtils.isBlank(authInfo.getBankCardNo()) ? "" : authInfo.getBankCardNo());
            person.setCerPersonImg(authInfo.getCertificateFront());
            person.setCerPersonHeadimg(authInfo.getAvatar());
            person.setCerPersonAddress(authInfo.getAddress());
            person.setCerPersonRange(authenticationInfoUtil.resolveBusinessTypeName(authInfo.getRfBusinessTypeName()));
            person.setCerPersonCreatetime(authInfo.getFirstTime());
            person.setCerPersonLevel(authInfo.getFkVipId() + "");
            person.setCerPersonEdit(editCount(authInfo.getFkUserId(), authInfo.getId()));
            person.setCerPersonPremises("");
            person.setCerPersonCon(authInfo.getCertificateBack());
            person.setCerPersonHand(authInfo.getCertificateHand());
            person.setCerPersonState(status);
            person.setCerPersonDetails("");
            updateInfo(update, person);
            oldCoreCerPersonMapper.insert(person);
            searchId = person.getCerPersonId();
        } else if (authInfo.getVipType() == 2) {
            var company = new OldCoreCerCompany();
            company.setCerUserId(authInfo.getFkUserId());
            company.setCerCompanyName(authInfo.getName());
            company.setCerCompanyNickname(authInfo.getNickname());
            company.setCerCompanyPhone(authInfo.getPhone());
            company.setCerCompanyIdcard(authInfo.getCertificateNo());
            company.setCerCompanyBank("");
            company.setCerCompanyBankcard(StringUtils.isBlank(authInfo.getBankCardNo()) ? "" : authInfo.getBankCardNo());
            company.setCerCompanyImg(authInfo.getCertificateFront());
            company.setCerCompanyHeadimg(authInfo.getAvatar());
            company.setCerCompanyAddress(authInfo.getAddress());
            company.setCerCompanyContact(authInfo.getPhone());
            company.setCerCompanyRange(authenticationInfoUtil.resolveBusinessTypeName(authInfo.getRfBusinessTypeName()));
            company.setCerCompanyLevel(authInfo.getFkVipId() + "");
            company.setCerCompanyEdit(editCount(authInfo.getFkUserId(), authInfo.getId()));
            company.setCerCompanyPremises("");
            company.setCerCompanyOther("");
            company.setCerCompanyCon(authInfo.getCertificateBack());
            company.setCerCompanyHand(authInfo.getCertificateHand());
            company.setCerCompanyProduct("");
            company.setCerCompanyAgent(authInfo.getLegalName());
            company.setCerCompanyAgentPhone(authInfo.getLegalPhone());
            company.setCerCompanyAgentFace(authInfo.getLegalCertificateFront());
            company.setCerCompanyAgentCon(authInfo.getLegalCertificateBack());
            company.setCerCompanyAgentHand(authInfo.getLegalCertificateHand());
            company.setCerCompanyIdentical(authInfo.getLegalCertificateFront().equals(authInfo.getCertificateFront()) ? "1" : "2");
            company.setCerCompanyAgentIdcard(authInfo.getLegalCertificateNo());
            company.setCerCompanyOther2("");
            company.setCerCompanyHonor("");
            company.setCerCompanyTag(DEFAULT_TAG);
            company.setCerCompanyState(status);
            company.setCerCompanyDetails("");
            updateInfo(update, company);
            if (StringUtils.isNotBlank(authInfo.getOtherLicense())) {
                var list = ListUtil.Spilt.to(authInfo.getOtherLicense(), ",");
                company.setCerCompanyOther(!list.isEmpty() ? list.getFirst() : "");
                company.setCerCompanyOther2(list.size() > 1 ? list.get(1) : "");
            } else {
                company.setCerCompanyOther("");
                company.setCerCompanyOther2("");
            }
            var store = getStoreByUserId(authInfo.getFkUserId());
            if (store != null) {
                company.setCerCompanyProduct(String.join(BaseInfoConstant.IMAGE_SPILT, ListUtil.Spilt.to(store.getProductUrls(), ",")));
                company.setCerCompanyPremises(String.join(BaseInfoConstant.IMAGE_SPILT, ListUtil.Spilt.to(store.getSceneUrls(), ",")));
            }
            oldCoreCerCompanyMapper.insert(company);
            searchId = company.getCerCompanyId();
        }
        // 新增
        var oldCoreCerMessage = new OldCoreCerMessage();
        oldCoreCerMessage.setCerMsgId(authInfo.getId());
        oldCoreCerMessage.setCerUserId(authInfo.getFkUserId());
        oldCoreCerMessage.setCerSearchId(searchId);
        oldCoreCerMessage.setCerMsgNickname(authInfo.getNickname());
        oldCoreCerMessage.setCerMsgPhone(authInfo.getPhone());
        oldCoreCerMessage.setCerLevelId(authInfo.getFkVipId());
        oldCoreCerMessage.setCerLevelName(authInfo.getRfVipName());
        oldCoreCerMessage.setCerOccupationId(null);
        oldCoreCerMessage.setCerOccupationName(null);
        oldCoreCerMessage.setCerType(authInfo.getVipType());
        oldCoreCerMessage.setCerMsgDate(authInfo.getFirstTime());
        oldCoreCerMessage.setCerMsgVerfiy(update.getAuditTime());
        oldCoreCerMessage.setCerMsgReason(update.getAuditRemark());
        oldCoreCerMessage.setCerCopeTime(LocalDateTime.now().minusMonths(1));
        oldCoreCerMessage.setCerDeadline(authInfo.getEndTime());
        oldCoreCerMessage.setCerMsgState(status);
        oldCoreCerMessage.setCerMsgCreateDate(authInfo.getGmtCreate());
        oldCoreCerMessageMapper.insert(oldCoreCerMessage);
        // 查询等级信息
        var oldCoreCerLevel = oldCoreCerLevelMapper.selectById(oldCoreCerMessage.getCerLevelId());
        // 新增其他信息
        var oldCoreCerLevelSub = new OldCoreCerLevelSub();
        oldCoreCerLevelSub.setCerMsgId(oldCoreCerMessage.getCerMsgId());
        oldCoreCerLevelSub.setCerLevelName(oldCoreCerLevel.getCerLevelName());
        oldCoreCerLevelSub.setCerLevelContent(oldCoreCerLevel.getCerLevelContent());
        oldCoreCerLevelSub.setCerLevelSecurity(oldCoreCerLevel.getCerLevelSecurity());
        oldCoreCerLevelSub.setCerLevelAnnual(oldCoreCerLevel.getCerLevelAnnual());
        var list = new ArrayList<String>();
        list.add(oldCoreCerLevel.getCerLevelName());
        list.add("认证");
        if (authInfo.getDeposit() != null && authInfo.getDeposit() > 0) {
            if (oldCoreCerLevel.getCerLevelContent().contains("押金10000元可退")) {
                list.add("押1万");
            }
            if (oldCoreCerLevel.getCerLevelContent().contains("押金2000元可退")) {
                list.add("押2千");
            }
            if (oldCoreCerLevel.getCerLevelContent().contains("押金1000元可退")) {
                list.add("押1千");
            }
        }
        oldCoreCerLevelSub.setCerAutName(String.join(",", list));
        oldCoreCerLevelSubMapper.insert(oldCoreCerLevelSub);
        if (update.getUpdateType() == UpdateAuthenticationInfoEnum.DEPOSIT.getType()) {
            resolveDeposit(update, authInfo, oldCoreCerMessage);
        }
    }

    /**
     * 修改
     *
     * @param trigger           触发器
     * @param authInfo          认证信息
     * @param update            更新数据
     * @param oldCoreCerMessage 旧版数据
     */
    private void update(SyncTrigger trigger, AuthenticationInfo authInfo, AuthenticationInfoUpdate update, OldCoreCerMessage oldCoreCerMessage) {
        var status = calcStatus(update);
        var searchId = oldCoreCerMessage.getCerSearchId();
        if (authInfo.getVipType() == 1) {
            var person = oldCoreCerPersonMapper.selectById(searchId);
            if (person == null) {
                throw new BusinessSimpleException(String.format("未查询到个人认证信息：%s", searchId));
            }
            person.setCerUserNickname(authInfo.getNickname());
            person.setCerPersonName(authInfo.getName());
            person.setCerPersonPhone(authInfo.getPhone());
            person.setCerPersonIdcard(authInfo.getCertificateNo());
            person.setCerPersonBankcard(StringUtils.isBlank(authInfo.getBankCardNo()) ? "" : authInfo.getBankCardNo());
            person.setCerPersonHeadimg(authInfo.getAvatar());
            person.setCerPersonImg(authInfo.getCertificateFront());
            person.setCerPersonAddress(authInfo.getAddress());
            person.setCerPersonRange(authenticationInfoUtil.resolveBusinessTypeName(authInfo.getRfBusinessTypeName()));
            person.setCerPersonLevel(authInfo.getFkVipId() + "");
            person.setCerPersonCon(authInfo.getCertificateBack());
            person.setCerPersonHand(authInfo.getCertificateHand());
            person.setCerPersonState(status);
            updateInfo(update, person);
            oldCoreCerPersonMapper.updateById(person);
        } else if (authInfo.getVipType() == 2) {
            var company = oldCoreCerCompanyMapper.selectById(searchId);
            if (company == null) {
                throw new BusinessSimpleException(String.format("未查询到企业认证信息：%s", searchId));
            }
            company.setCerCompanyName(authInfo.getName());
            company.setCerCompanyNickname(authInfo.getNickname());
            company.setCerCompanyPhone(authInfo.getPhone());
            company.setCerCompanyIdcard(authInfo.getCertificateNo());
            company.setCerCompanyBankcard(StringUtils.isBlank(authInfo.getBankCardNo()) ? "" : authInfo.getBankCardNo());
            company.setCerCompanyHeadimg(authInfo.getAvatar());
            company.setCerCompanyAddress(authInfo.getAddress());
            company.setCerCompanyContact(authInfo.getPhone());
            company.setCerCompanyRange(authenticationInfoUtil.resolveBusinessTypeName(authInfo.getRfBusinessTypeName()));
            company.setCerCompanyLevel(authInfo.getFkVipId() + "");
            company.setCerCompanyEdit(editCount(authInfo.getFkUserId(), authInfo.getId()));
            company.setCerCompanyImg(authInfo.getCertificateFront());
            company.setCerCompanyCon(authInfo.getCertificateBack());
            company.setCerCompanyHand(authInfo.getCertificateHand());
            company.setCerCompanyAgent(authInfo.getLegalName());
            company.setCerCompanyAgentPhone(authInfo.getLegalPhone());
            company.setCerCompanyAgentFace(authInfo.getLegalCertificateFront());
            company.setCerCompanyAgentCon(authInfo.getLegalCertificateBack());
            company.setCerCompanyAgentHand(authInfo.getLegalCertificateHand());
            company.setCerCompanyIdentical(authInfo.getLegalCertificateFront().equals(authInfo.getCertificateFront()) ? "1" : "2");
            company.setCerCompanyAgentIdcard(authInfo.getLegalCertificateNo());
            company.setCerCompanyState(status);
            updateInfo(update, company);
            if (StringUtils.isNotBlank(authInfo.getOtherLicense())) {
                var list = ListUtil.Spilt.to(authInfo.getOtherLicense(), ",");
                company.setCerCompanyOther(!list.isEmpty() ? list.getFirst() : "");
                company.setCerCompanyOther2(list.size() > 1 ? list.get(1) : "");
            } else {
                company.setCerCompanyOther("");
                company.setCerCompanyOther2("");
            }
            var store = getStoreByUserId(authInfo.getFkUserId());
            if (store != null) {
                company.setCerCompanyProduct(String.join(BaseInfoConstant.IMAGE_SPILT, ListUtil.Spilt.to(store.getProductUrls(), ",")));
                company.setCerCompanyPremises(String.join(BaseInfoConstant.IMAGE_SPILT, ListUtil.Spilt.to(store.getSceneUrls(), ",")));
            }
            oldCoreCerCompanyMapper.updateById(company);
        }
        // 新增
        oldCoreCerMessage.setCerMsgNickname(authInfo.getNickname());
        oldCoreCerMessage.setCerMsgPhone(authInfo.getPhone());
        oldCoreCerMessage.setCerLevelId(authInfo.getFkVipId());
        oldCoreCerMessage.setCerLevelName(authInfo.getRfVipName());
        oldCoreCerMessage.setCerType(authInfo.getVipType());
        oldCoreCerMessage.setCerMsgVerfiy(update.getAuditTime());
        oldCoreCerMessage.setCerMsgReason(update.getAuditRemark());
        oldCoreCerMessage.setCerMsgState(status);
        oldCoreCerMessageMapper.updateById(oldCoreCerMessage);
        // 查询等级信息
        var oldCoreCerLevel = oldCoreCerLevelMapper.selectById(oldCoreCerMessage.getCerLevelId());
        // 新增其他信息
        var oldCoreCerLevelSub = new OldCoreCerLevelSub();
        oldCoreCerLevelSub.setCerMsgId(oldCoreCerMessage.getCerMsgId());
        oldCoreCerLevelSub.setCerLevelName(oldCoreCerLevel.getCerLevelName());
        oldCoreCerLevelSub.setCerLevelContent(oldCoreCerLevel.getCerLevelContent());
        oldCoreCerLevelSub.setCerLevelSecurity(oldCoreCerLevel.getCerLevelSecurity());
        oldCoreCerLevelSub.setCerLevelAnnual(oldCoreCerLevel.getCerLevelAnnual());
        var list = new ArrayList<String>();
        list.add(oldCoreCerLevel.getCerLevelName());
        list.add("认证");
        if (authInfo.getDeposit() != null && authInfo.getDeposit() > 0) {
            if (oldCoreCerLevel.getCerLevelContent().contains("押金10000元可退")) {
                list.add("押1万");
            }
            if (oldCoreCerLevel.getCerLevelContent().contains("押金2000元可退")) {
                list.add("押2千");
            }
            if (oldCoreCerLevel.getCerLevelContent().contains("押金1000元可退")) {
                list.add("押1千");
            }
        }
        oldCoreCerLevelSub.setCerAutName(String.join(",", list));
        oldCoreCerLevelSubMapper.insert(oldCoreCerLevelSub);
        if (update.getUpdateType() == UpdateAuthenticationInfoEnum.DEPOSIT.getType()) {
            resolveDeposit(update, authInfo, oldCoreCerMessage);
        }
    }

    /**
     * 根据更新数据获取状态
     *
     * @param update 更新数据
     * @return 状态
     */
    private int calcStatus(AuthenticationInfoUpdate update) {
        return switch (update.getAuditStatus()) {
            case AuthInfoConstant.NewUpdateStatus.WAIT -> AuthInfoConstant.OldStatus.MODIFY_INFO;
            case AuthInfoConstant.NewUpdateStatus.PASS -> AuthInfoConstant.OldStatus.PASS;
            case AuthInfoConstant.NewUpdateStatus.REJECT -> AuthInfoConstant.OldStatus.MODIFY_INFO_REJECT;
            default -> throw new BusinessSimpleException("未知的审核状态");
        };
    }

    /**
     * 根据用户id获取认证信息
     *
     * @param userId 用户id
     * @return 认证信息
     */
    private AuthenticationInfo getAuthInfoByUserId(Long userId) {
        var wrapper = new LambdaQueryWrapper<AuthenticationInfo>()
                .eq(AuthenticationInfo::getFkUserId, userId)
                .last("limit 1");
        return authenticationInfoMapper.selectOne(wrapper);
    }

    /**
     * 查询修改的次数信息
     *
     * @param userId    用户id
     * @param excludeId 排除的id
     * @return 修改的次数
     */
    private int editCount(Long userId, Long excludeId) {
        var wrapper1 = new LambdaQueryWrapper<AuthenticationInfoHistory>()
                .ne(AuthenticationInfoHistory::getVipType, 0)
                .eq(AuthenticationInfoHistory::getFkUserId, userId);
        var count1 = authenticationInfoHistoryMapper.selectCount(wrapper1);
        var wrapper2 = new LambdaQueryWrapper<AuthenticationInfoUpdate>()
                .ne(excludeId != null, AuthenticationInfoUpdate::getId, excludeId)
                .eq(AuthenticationInfoUpdate::getFkUserId, userId);
        var count2 = authenticationInfoUpdateMapper.selectCount(wrapper2);
        return (int) (count1 + count2);
    }

    /**
     * 根据修改的数据类型，修改对应的信息
     *
     * @param update           修改的内容
     * @param oldCoreCerPerson 认证信息
     */
    private void updateInfo(AuthenticationInfoUpdate update, OldCoreCerPerson oldCoreCerPerson) {
        var method = PERSON_MAP.get(UpdateAuthenticationInfoEnum.getEnumByType(update.getUpdateType()));
        if (method == null) {
            return;
        }
        method.accept(oldCoreCerPerson, update.getUpdateContent());
    }

    /**
     * 根据修改的数据类型，修改对应的信息
     *
     * @param update            修改的内容
     * @param oldCoreCerCompany 认证信息
     */
    private void updateInfo(AuthenticationInfoUpdate update, OldCoreCerCompany oldCoreCerCompany) {
        var method = COMPANY_MAP.get(UpdateAuthenticationInfoEnum.getEnumByType(update.getUpdateType()));
        if (method == null) {
            return;
        }
        method.accept(oldCoreCerCompany, update.getUpdateContent());
    }

    /**
     * 处理押金信息
     *
     * @param update  更新数据
     * @param message 旧版数据
     */
    private void resolveDeposit(AuthenticationInfoUpdate update, AuthenticationInfo authInfo, OldCoreCerMessage message) {
        // 获取押金订单信息
        if (StringUtils.isBlank(authInfo.getDepositTradeNo())) {
            log.debug("押金订单信息不存在：{}", authInfo.getId());
            return;
        }
        var billWrapper = new LambdaQueryWrapper<BillHistory>().eq(BillHistory::getOutTradeNo, authInfo.getDepositTradeNo());
        var bill = billHistoryMapper.selectOne(billWrapper);
        if (bill == null) {
            log.debug("押金订单信息不存在：{}", authInfo.getId());
            return;
        }
        var total = new BigDecimal(bill.getTotalAmount()).divide(ConstantNumber.BIG_DECIMAL_HUNDRED, 2, RoundingMode.HALF_UP).doubleValue();
        var oldCoreCerPayDeposit = new OldCoreCerPayDeposit();
        oldCoreCerPayDeposit.setCerOrderNo(bill.getOutTradeNo());
        oldCoreCerPayDeposit.setCerPayPhone(authInfo.getPhone());
        oldCoreCerPayDeposit.setCerMsgId(message.getCerMsgId());
        oldCoreCerPayDeposit.setCerUserId(message.getCerUserId());
        oldCoreCerPayDeposit.setCerPayChannel(PAY_METHOD_MAP.get(bill.getTradeType()));
        oldCoreCerPayDeposit.setCerPayTotal(total);
        oldCoreCerPayDeposit.setCerOrderId(bill.getOutTradeNo());
        oldCoreCerPayDeposit.setCerPayDate(bill.getGmtCreate());
        oldCoreCerPayDeposit.setCerPayType(1);
        oldCoreCerPayDeposit.setCerPayState(1);
        oldCoreCerPayDeposit.setCerPayEnd(null);
        oldCoreCerPayDepositMapper.insert(oldCoreCerPayDeposit);
        var oldCoreCerPayLogDeposit = new OldCoreCerPayLogDeposit();
        oldCoreCerPayLogDeposit.setPayId(oldCoreCerPayDeposit.getCerPayId());
        oldCoreCerPayLogDeposit.setPayUserId(message.getCerUserId());
        oldCoreCerPayLogDeposit.setPayTotal(total);
        oldCoreCerPayLogDeposit.setPayTime(bill.getGmtCreate());
        oldCoreCerPayLogDepositMapper.insert(oldCoreCerPayLogDeposit);
    }

    /**
     * 根据用户id获取店铺信息
     *
     * @param userId 用户id
     * @return 店铺信息
     */
    private AuthenticationInfoStore getStoreByUserId(Long userId) {
        var wrapper = new LambdaQueryWrapper<AuthenticationInfoStore>()
                .eq(AuthenticationInfoStore::getFkUserId, userId);
        return authenticationInfoStoreMapper.selectOne(wrapper);
    }

    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "authentication_info_update";
    }

}
